/*
 * @(#)ErrorSqlMailTask.java   1.0  2022年1月11日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.task;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.swift.dts.dao.ErrorSqlTableDao;
import com.swift.dts.loadconfig.FileSysConfig;
import com.swift.dts.loadconfig.model.SysConfig;
import com.swift.dts.util.MailUtil;
import com.swift.dts.util.MailUtil.MainContent;
import com.swift.dts.util.MailUtil.MainSendUser;
import com.swift.util.date.DateUtil;
import com.swift.util.type.TypeUtil;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2022年1月11日
 */
@Component
public class ErrorSqlMailTask {

    @Autowired
    private ErrorSqlTableDao errorSqlTableDao;
    
    @Autowired
    private FileSysConfig fileSysConfig;
    
    @Scheduled(cron="0 0 9 ? * *")
    public void doUpdate() {
        SysConfig sysConfig = fileSysConfig.read();
        if(sysConfig==null) return;
        MainSendUser mainSendUser = sysConfig.getMainSendUser();
        if(!hasMailNotice(mainSendUser)) return;
        int errorSqlCount = errorSqlTableDao.count(null);
        if(errorSqlCount<=0) return;
        MainContent content = new MainContent();
        content.setTitle("DTS异常SQL提示"+DateUtil.formatDate(new Date(), "yyyy-MM-dd"));
        content.setContent("当前未处理异常SQL数量为:"+errorSqlCount);
        MailUtil.sendMain(mainSendUser, content);
    }
    
    private boolean hasMailNotice(MainSendUser mainSendUser) {
        if(mainSendUser==null) return false;
        if(TypeUtil.isNull(mainSendUser.getMailHost()))  return false;
        if(TypeUtil.isNull(mainSendUser.getSendMail()))  return false;
        if(TypeUtil.isNull(mainSendUser.getSendPassword()))  return false;
        if(TypeUtil.isNull(mainSendUser.getToMain()))  return false;
        return true;
    }
}
